<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_search
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

$helper = JPATH_COMPONENT.'/helpers/glycophile.php';
require_once($helper);

/**
 *
 * @package		Joomla.Site
 * @subpackage	com_glycophile
 * @since 1.5
 */
class GlycophileModelSeries extends JModelLegacy
{
	/**
	 * data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * Helper
	 *
	 * @var object
	 */
	var $_helper = null;
	
	var $_theme = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		//Get configuration
		$app	= JFactory::getApplication();
		$config = JFactory::getConfig();
		
		$this->_helper = new GlycophileHelper();

		// Get the pagination request variables
		$this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'uint'));
		$this->setState('limitstart', JRequest::getUInt('limitstart', 0));
	
		$this->countAllRows();
		$this->getList();
		$this->_theme = $this->_helper->getThemesById(JRequest::getVar('theme', 0));
	}

	/**
	 * Method to get Series list
	 * Si theme sélectionné  (exemple Sujet divers id = 16 )
	 *
	 * @access public
	 * @return array
	 */
	function getData()
	{
		return $this->_data;
	}
	
	function getTheme()
	{
		return $this->_theme;
	}
	
	function getTotal()
	{
		return $this->_total;
	}
	
	function getPagination()
	{
		return $this->_pagination;
	}
	
	function getQuery($id_theme = 0, $id_pays = 0, $orderBy = null, $order = 'ASC'){
		$query = "SELECT s.*,  s.id_theme, COUNT(DISTINCT(ss.id)) AS variante, s.datemodif , th.code, th.theme, ss.id as id_ss
		FROM #__glycophile_serie s, #__glycophile_sousserie ss,  #__glycophile_theme th
		WHERE ss.state = 1 AND s.state = 1"; // AND t.state = 1";
		$query .= " AND ss.id_serie = s.id"; 
	//	$query .= " AND s.id_tome = t.id";
		$query .= " AND s.id_theme = th.id";
    $query .= $id_pays != 0 ? " AND s.id_pays = ".$id_pays : '';
    $query .= $id_pays == 0 ? " AND s.id_pays != 1 " : '';
    $query .= $id_pays == 1  ? " AND s.calcul_serie > 0" : '';

//		$query .= $id_pays == 1 ? '' : " AND t.typeSucre = 'E'";
		$query .= $id_theme != 0 ? " AND s.id_theme = ". $id_theme : '' ;
		$query .= " GROUP BY s.id";
		$query .= !is_null( $orderBy ) ? " ORDER BY ". $orderBy ." ". $order : '';
	//	echo $query;
    
		return $query ;
	}
	
	function countAllRows(){
		$db = $this->getDbo();
		$id_theme = JRequest::getVar('theme', 0);
		$orderBy = JRequest::getVar('orderBy', 's.page');
		$order = JRequest::getVar('order', 'DESC');
    $id_pays = JRequest::getVar('pays', 0);
		$query = $this->getQuery($id_theme, $id_pays, $orderBy, $order);
		$db->setQuery($query);
		$items = $db->query();
		$this->_total = $db->getNumRows();
	}
	
	function getList(){
		$db = $this->getDbo();
		global $mainframe;
		//Get configuration
		$config = JFactory::getConfig();
		// Get the pagination request variables
		
		$limitstart = JRequest::getUInt('limitstart', 1);
		$limit = $config->getValue('config.list_limit');
		$ligne = ($limitstart - 1) * $limit;
		$id_theme = JRequest::getVar('theme', 0);
    $id_pays = JRequest::getVar('pays', 0);

    if($id_theme != 0){
      $orderBy = JRequest::getVar('orderBy', 's.page');
    } else {
      $orderBy = JRequest::getVar('orderBy', 's.dateCreation');
    }
	
    
		$order = JRequest::getVar('order', 'DESC');
		$query = $this->getQuery($id_theme, $id_pays, $orderBy, $order);
		$query .= " Limit ". $ligne .", ". $limit; 
		
		$db->setQuery($query);
		$series = $db->loadObjectList();
		
		foreach($series as $serie){
			$query = "Select * From #__glycophile_sousserie ss";
			$query .= "	Where id_serie = ".$serie->id;
			$db->setQuery($query);
			$sseries = $db->loadObjectList();
			$serie->sseries = $sseries;
		}
		
		$this->_data = $series;
		
		$Itemid = JRequest::getVar('Itemid', 0);
		$url = 'index.php?option=com_glycophile&view=series&pays='.$id_pays.'&theme='. $id_theme .'&Itemid='.$Itemid;
		$url .= '&orderBy='. $orderBy .'&order='.$order;
		$this->_pagination = $this->_helper->getPaginationBar($this->_total, $limit, $url);
	}

}
